home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 19 code / Truffles Sample / Bitmap Shape with Clip.c next >
Encoding:
C/C++ Source or Header  |  1994-07-21  |  8.6 KB  |  273 lines  |  [TEXT/KAHL]

  1. /**
  2.  --
  3.  --        App:        Bitmap Shape with Clip
  4.  --
  5.  -- 
  6.  --        File:        Bitmap Shape with Clip.c
  7.  --
  8.  --
  9.  --        Comments:    This code the shows off the ability of QuickDraw GX to clip any shape with any geometric shape. 
  10.  --                    In this case, we retrieve a bitmap shape from the resource fork of the application, 
  11.  --                    and clip it with a text shape - "BAY". We collect both shapes into a GX picture, 
  12.  --                    thereby allowing us to make one call to draw both shapes.
  13.  --
  14.  --
  15.  --        Version:    
  16.  --                            6/94    dkj Modified to match code in column in develop #19
  17.  --
  18.  --
  19. --                            1.0     4/94:    added the capability to dynamically determine the amount of
  20.  --                                    scaling required to make the clip shape clip the entire bitmap
  21.  --                                    shape.    
  22.  --
  23.  --                             3/93:    created 
  24.  --
  25.   --        Components:    Bitmap Shape with Clip.c
  26.  --                    graphics shell.c
  27.  --                    graphics shell.h
  28.  --
  29.  --
  30.  --        QuickDraw GX
  31.  --        Libraries
  32.  --        Used:        This application uses the following QuickDraw GX library code files:
  33.  --                    "color library.c", "font library.c", "graphics debug library.c",
  34.  --                    "qd library.c", and "transform library.c". 
  35.  --        
  36.  --        
  37.  --        Notes:        1) Print this file in landscape for the best results
  38.  --                    2) If you are using THINK C v5.x, I have added THINK markers to navigate the code.
  39.  --                    3) This code was adapted from the "One Rectangle" QuickDraw GX sample.
  40.  --
  41.  --
  42.  --        Author:        Pete "Luke" Alexander
  43.  --                    Developer Technical Support
  44.  --                    AppleLink: DEVSUPPORT
  45.  --
  46.  --        
  47.  --        ©1992 - 1994  Apple Computer, Inc. 
  48.  --
  49.  **/
  50.  
  51. #include <events.h>
  52. #include <windows.h>
  53.  
  54. #include "Font library.h" 
  55. #include "graphics debugging.h"
  56. #include "graphics libraries.h"
  57. #include "graphics toolbox.h"
  58. #include "qd library.h"
  59. #include "graphics shell.h"
  60.  
  61. #define kCheckBitmapShape        // if defined, validates the bitmap shape
  62. //#define useDifferenceMethod    // if defined, does it the long way
  63.  
  64. // The id of the pixmap in the resource fork
  65. #define kPixMapID            128
  66.  
  67. //
  68. //  Set up the title and size of the window 
  69. //
  70. Str255         gWindowTitle = "\p Nifty Clip: click to move on";
  71. Rect         gWindowQDRect  = {50, 20, 345, 455};
  72.  
  73. //
  74. //    gGraphicsHeapSize sets the size of the graphics gxHeap created by calling the GXNewGraphicsClient routine
  75. //    in main () within graphics shell.c.  You can determine the amount of graphics gxHeap required by using GraphicsBug.
  76. //    With  gGraphicsHeapSize set to 48k,I had 6 free blocks left in the graphics gxHeap and
  77. //    I was not receiving any memory related warnings or notices from GX....
  78. //
  79. long        gGraphicsHeapSize = 48;
  80.  
  81. gxShape     gthePicture;
  82.  
  83. // •••••••••••• Added 5/31/94 dkj
  84. Boolean        gShowSteps = true;    // if true, shows intermediate steps
  85.  
  86. /*------ ShowUntilClick ---------------------------------------------------------------------------------*/
  87. // Just shows the indicated shapes (max of two) and waits for a click before returning. A SickHack™.
  88. void ShowUntilClick(gxShape oneShape, gxShape otherShape);
  89.  
  90. void ShowUntilClick(gxShape oneShape, gxShape otherShape)
  91. {
  92.     SetPort(gWindow);
  93.     EraseRect(&gWindow->portRect);
  94.     if(oneShape != nil) GXDrawShape(oneShape);
  95.     if(otherShape != nil) GXDrawShape(otherShape);
  96.     while(!Button());     // Let user see it until they click
  97.     while(WaitMouseUp()); // Wait 'til they let up
  98. }
  99.  
  100. //  ••••••••••••••• End additions
  101.  
  102. /*------ DoInitialization ---------------------------------------------------------------------------------*/
  103.  
  104. void DoInitialization(gWindow)
  105. WindowPtr gWindow;
  106. {
  107.     gxShape        theClip = nil, clipShapeOutline;
  108.     gxShape        theBitmap = nil;
  109.     Fixed        xScale, yScale;
  110.     Fixed         textWidth,textHeight;
  111.     Fixed         bitmapWidth, bitmapHeight;
  112.     gxRectangle    bitmapBounds, textBounds;
  113.  
  114.     InitCommonColors ();
  115.     
  116.     //
  117.     //    Create the picture shape to hold our clipped bitmap and outline (path) shape
  118.     //
  119.     gthePicture = GXNewShape(gxPictureType);
  120.  
  121.     //
  122.     //    Retrieve the bitmap shape form the resource fork of the application. With the "debugging" init installed, we
  123.     //    can check to see if the shape was retrieved correctly by calling the shape validation routine. If the shape is
  124.     //    not valid, we will recieve a warning in the debugger.
  125.     //
  126.     theBitmap = GetPixMapShape(kPixMapID);
  127.     
  128.     #ifdef kCheckBitmapShape
  129.         GXValidateShape (theBitmap);
  130.     #endif
  131.  
  132.     //
  133.     //    Define the text shape to clip our bitmap shape with. In this case, our clip shape
  134.     //     is a text shape (12 point Helvetica by default).
  135.     //
  136.     theClip = GXNewText( 3, (unsigned char*)"BAY", nil );
  137.     
  138.     
  139.     // ••••• Added by dkj for develop
  140.     if(gShowSteps)
  141.         ShowUntilClick(theBitmap, theClip);
  142.         
  143.     #ifdef useDifferenceMethod
  144.         //
  145.         //    Determine the bounds of our bitmap and clip shape and move the clip shape to the 
  146.         //    top left corner of the bitmap shape. 
  147.         //
  148.         GXGetShapeBounds( theBitmap, 0, &bitmapBounds );
  149.         GXGetShapeBounds( theClip, 0, &textBounds );
  150.         textHeight = textBounds.bottom - textBounds.top;
  151.         GXMoveShapeTo ( theClip, bitmapBounds.left, bitmapBounds.top + textHeight );
  152.         
  153.     // ••••• Added by dkj for develop
  154.     if(gShowSteps)
  155.         ShowUntilClick(nil, theClip);
  156.         
  157.         //
  158.         //    We turn off the metrics and contour capabilites of TrueType to enable GX to
  159.         //    scale the clip shape linearly. We convert our text shape to a primitive shape because
  160.         //    you cannot use a text shape as a clip shape. A clip shape can only be a primitive shape
  161.         //
  162.         GXSetShapeTextAttributes ( theClip, gxNoMetricsGridText | gxNoContourGridText );
  163.         GXPrimitiveShape ( theClip );
  164.  
  165.         //
  166.         //    We can now determine the amount we need to scale our clip shape to cover our bitmap
  167.         //    shape. This is accomplished by determining the differences between the height and width
  168.         //    of the bitmap and clip shapes. We will then use these differences as the scale factors
  169.         //    when we call GXScaleShape (..) to scale up our clip shape.
  170.         //
  171.         bitmapWidth = bitmapBounds.right - bitmapBounds.left;
  172.         textWidth = textBounds.right - textBounds.left;
  173.         xScale = FixedDivide( bitmapWidth, textWidth );
  174.     
  175.         bitmapHeight = bitmapBounds.bottom - bitmapBounds.top;
  176.         textHeight = textBounds.bottom - textBounds.top;
  177.         yScale = FixedDivide ( bitmapHeight,  textHeight );
  178.         
  179.         GXScaleShape( theClip, xScale,  yScale, bitmapBounds.left, bitmapBounds.top );
  180.  
  181.     #else
  182.         GXSetShapeTextAttributes ( theClip, gxNoMetricsGridText | gxNoContourGridText );
  183.         GXGetShapeBounds( theBitmap, 0, &bitmapBounds );
  184.         GXSetShapeBounds( theClip, &bitmapBounds );
  185.     #endif
  186.  
  187.     // ••••• Added by dkj for develop
  188.     if(gShowSteps)
  189.         ShowUntilClick(theBitmap, theClip);
  190.  
  191.     //
  192.     //     Set the clip of our bitmap shape to our text shape and add it to our picture.
  193.     //
  194.     GXSetShapeClip( theBitmap, theClip );
  195.     
  196.     // ••••• Added by dkj for develop
  197.     if(gShowSteps)
  198.         ShowUntilClick(theBitmap, nil);
  199.  
  200.     GXSetPictureParts(gthePicture, 0, 0, 1, &theBitmap, nil, nil, nil );
  201.     GXDisposeShape ( theBitmap );
  202.  
  203.     //
  204.     //    Change the fill of our text shape be to the outline of the text,
  205.     //    set the size used, set the pen to cruise on the outside
  206.     //    of the contour of each letter, and add it to our picture shape.
  207.     //
  208.     GXSetShapeFill( theClip, gxClosedFrameFill );
  209.     GXSetShapeStyleAttributes(theClip, gxOutsideFrameStyle);
  210.     GXSetShapePen( theClip, ff(3));
  211.     SetShapeCommonColor( theClip, blue);
  212.  
  213.     
  214.     // ••••• Added by dkj for develop
  215.     if(gShowSteps)
  216.         ShowUntilClick(theClip, nil);
  217.  
  218.     GXSetPictureParts( gthePicture, 0, 0, 1, &theClip, nil, nil, nil );
  219.     GXDisposeShape ( theClip );
  220.  
  221.     GXMoveShape(gthePicture, ff(20), ff(15) );    
  222.     
  223.     // ••••• Added by dkj for develop
  224.     if(gShowSteps)
  225.         ShowUntilClick(gthePicture, nil);
  226. }
  227.  
  228.  
  229. /*------ DoDraw ---------------------------------------------------------------------------------------*/
  230.  
  231. void DoDraw(gWindow)
  232. WindowPtr gWindow;
  233. {
  234.      GXDrawShape (gthePicture);
  235. }
  236.  
  237.  
  238. /*------ DoDispose -------------------------------------------------------------------------------------*/
  239.  
  240. void DoDispose(gWindow)
  241. WindowPtr gWindow;
  242. {
  243.     /**  
  244.         You should always dispose of your GX graphics objects before tossing your window. Why? It's generally good 
  245.         form and this approach guarantees that everything is disposed. If you had not disposed of everything, the
  246.         call to DisposeWindow should dispose of the objects. If you are running the debugging version of the 
  247.         SecretGraphics init with notices set, you will receive a notice that you had not disposed of everything. You
  248.         can turn notices on in this file by setting gDebugging = TRUE (above).
  249.     **/
  250.     DisposeCommonColors ();
  251.     GXDisposeShape(gthePicture);  
  252.      GXDisposeShape(gWindowBoundsShape);  
  253.     DisposeWindow(gWindow);
  254. }
  255.     
  256.  
  257.  
  258. /*------ DoClick ---------------------------------------------------------------------------------------*/
  259.  
  260. void DoClick( orgMouseLoc, theWindow )
  261. gxPoint        orgMouseLoc;
  262. WindowPtr     theWindow;
  263. {
  264. }
  265.  
  266.  
  267. /*------ DoIdle ----------------------------------------------------------------------------------------*/
  268.  
  269. void DoIdle(gWindow)
  270. WindowPtr gWindow;
  271. {
  272. }
  273.